﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServEvo.ServConfig
{
    class ConfigurationData
    {
        private static ConfigurationData manager;
        private IniFile configurationFile;
        private IniFile.IniSection mainConfigSection;
        private Dictionary<string, IniFile.IniSection> _listManagers;

        private bool bSysTray;
        private bool bUseBuildIn;
        private string sInstallDir;
        private string sHTTPManager;
        private string sPHPManager;
        private string sMySQLManager;
        private string sFTPManager;
        private string sSMTPManager;

        public bool SysTray
        {
            get { return bSysTray; }
            set
            {
                mainConfigSection.GetKey("SysTray").Value = value.ToString();
                bSysTray = value;
            }
        }
        public bool UseBuildIn
        {
            get { return bUseBuildIn; }
            set
            {
                mainConfigSection.GetKey("UseBuildIn").Value = value.ToString();
                bUseBuildIn = value;
            }
        }
        public string InstallDir
        {
            get { return sInstallDir; }
            set
            {
                mainConfigSection.GetKey("InstallDir").Value = value.ToString();
                sInstallDir = value;
            }
        }
        public string HTTPManager
        {
            get { return sHTTPManager; }
            set
            {
                IniFile.IniSection.IniKey key = mainConfigSection.GetKey("HTTPManager");
                if (key == null)
                {
                    key = mainConfigSection.AddKey("HTTPManager");
                }
                if (value.IndexOf(";") > 0)
                {
                    string manager = value.Split(';')[0];
                    string installDir = value.Split(';')[1];

                    if (installDir.Length > 0)
                    {
                        if (!_listManagers.ContainsKey(manager))
                        {
                            _listManagers.Add(manager, configurationFile.AddSection(manager));
                        }
                        if (_listManagers[manager].GetKey("InstallDir") == null)
                        {
                            _listManagers[manager].AddKey("InstallDir").Value = installDir;
                        }
                        else
                        {
                            _listManagers[manager].GetKey("InstallDir").Value = installDir;
                        }

                        key.Value = manager;
                        sHTTPManager = manager;
                    }
                    else
                    {
                        key.Value = " ";
                        sHTTPManager = string.Empty;
                    }
                }
                else
                {
                    key.Value = " ";
                    sHTTPManager = string.Empty;
                }
            }
        }
        public string PHPManager
        {
            get { return sPHPManager; }
            set
            {
                IniFile.IniSection.IniKey key = mainConfigSection.GetKey("PHPManager");
                if (key == null)
                {
                    key = mainConfigSection.AddKey("PHPManager");
                }

                if (value.IndexOf(";") > 0)
                {
                    string manager = value.Split(';')[0];
                    string installDir = value.Split(';')[1];

                    if (installDir.Length > 0)
                    {
                        if (!_listManagers.ContainsKey(manager))
                        {
                            _listManagers.Add(manager, configurationFile.AddSection(manager));
                        }
                        if (_listManagers[manager].GetKey("InstallDir") == null)
                        {
                            _listManagers[manager].AddKey("InstallDir").Value = installDir;
                        }
                        else
                        {
                            _listManagers[manager].GetKey("InstallDir").Value = installDir;
                        }

                        key.Value = manager;
                        sPHPManager = manager;
                    }
                    else
                    {
                        key.Value = " ";
                        sPHPManager = string.Empty;
                    }
                }
                else
                {
                    key.Value = " ";
                    sPHPManager = string.Empty;
                }
            }
        }
        public string MySQLManager
        {
            get { return sMySQLManager; }
            set
            {
                IniFile.IniSection.IniKey key = mainConfigSection.GetKey("MySQLManager");
                if (key == null)
                {
                    key = mainConfigSection.AddKey("MySQLManager");
                }

                if (value.IndexOf(";") > 0)
                {
                    string manager = value.Split(';')[0];
                    string installDir = value.Split(';')[1];

                    if (installDir.Length > 0)
                    {
                        if (!_listManagers.ContainsKey(manager))
                        {
                            _listManagers.Add(manager, configurationFile.AddSection(manager));
                        }
                        if (_listManagers[manager].GetKey("InstallDir") == null)
                        {
                            _listManagers[manager].AddKey("InstallDir").Value = installDir;
                        }
                        else
                        {
                            _listManagers[manager].GetKey("InstallDir").Value = installDir;
                        }


                        key.Value = manager;
                        sMySQLManager = manager;
                    }
                    else
                    {
                        key.Value = " ";
                        sMySQLManager = string.Empty;
                    }
                }
                else
                {
                    key.Value = " ";
                    sMySQLManager = string.Empty;
                }
            }
        }
        public string FTPManager
        {
            get { return sFTPManager; }
            set
            {
                IniFile.IniSection.IniKey key = mainConfigSection.GetKey("FTPManager");
                if (key == null)
                {
                    key = mainConfigSection.AddKey("FTPManager");
                }

                if (value.IndexOf(";") > 0)
                {
                    string manager = value.Split(';')[0];
                    string installDir = value.Split(';')[1];

                    if (installDir.Length > 0)
                    {
                        if (!_listManagers.ContainsKey(manager))
                        {
                            _listManagers.Add(manager, configurationFile.AddSection(manager));
                        }
                        if (_listManagers[manager].GetKey("InstallDir") == null)
                        {
                            _listManagers[manager].AddKey("InstallDir");
                        }
                        _listManagers[manager].GetKey("InstallDir").Value = installDir;

                        key.Value = manager;
                        sFTPManager = manager;
                    }
                    else
                    {
                        key.Value = " ";
                        sFTPManager = string.Empty;
                    }
                }
                else
                {
                    key.Value = " ";
                    sFTPManager = string.Empty;
                }
            }
        }
        public string SMTPManager
        {
            get { return sSMTPManager; }
            set
            {
                IniFile.IniSection.IniKey key = mainConfigSection.GetKey("SMTPManager");
                if (key == null)
                {
                    key = mainConfigSection.AddKey("SMTPManager");
                }

                if (value.IndexOf(";") > 0)
                {
                    string manager = value.Split(';')[0];
                    string installDir = value.Split(';')[1];

                    if (installDir.Length > 0)
                    {
                        if (!_listManagers.ContainsKey(manager))
                        {
                            _listManagers.Add(manager, configurationFile.AddSection(manager));
                        }
                        if (_listManagers[manager].GetKey("InstallDir") == null)
                        {
                            _listManagers[manager].AddKey("InstallDir").Value = installDir;
                        }
                        else
                        {
                            _listManagers[manager].GetKey("InstallDir").Value = installDir;
                        }


                        key.Value = manager;
                        sSMTPManager = manager;
                    }
                    else
                    {
                        key.Value = " ";
                        sSMTPManager = string.Empty;
                    }
                }
                else
                {
                    key.Value = " ";
                    sSMTPManager = string.Empty;
                }
            }
        }
        public Dictionary<string, IniFile.IniSection> ListManagers
        {
            get { return _listManagers; }
        }

        public void refrechData()
        {
            initListManagers();
            loadMainData();
        }
        private void loadMainData()
        {
            if (configurationFile.GetSection("ServEvo") == null)
                mainConfigSection = configurationFile.AddSection("ServEvo");
            else
                mainConfigSection = configurationFile.GetSection("ServEvo");

            if (mainConfigSection.GetKey("SysTray") == null
                || mainConfigSection.GetKey("SysTray").Value == null)
            {
                mainConfigSection.AddKey("SysTray").Value = "true";
                bSysTray = true;
            }
            else
                bSysTray = Convert.ToBoolean(mainConfigSection.GetKey("SysTray").Value.Trim());

            if (mainConfigSection.GetKey("UseBuildIn") == null
                || mainConfigSection.GetKey("UseBuildIn").Value == null)
            {
                mainConfigSection.AddKey("UseBuildIn").Value = "false";
                bUseBuildIn = false;
            }
            else
                bUseBuildIn = Convert.ToBoolean(mainConfigSection.GetKey("UseBuildIn").Value.Trim());


            if (mainConfigSection.GetKey("InstallDir") == null
                || mainConfigSection.GetKey("InstallDir").Value == null)
            {
                mainConfigSection.AddKey("InstallDir").Value = @"bin\";
                sInstallDir = @"bin\";
            }
            else
                sInstallDir = mainConfigSection.GetKey("InstallDir").Value.Trim();


            if (mainConfigSection.GetKey("HTTPManager") == null
                || mainConfigSection.GetKey("HTTPManager").Value == null)
            {
                mainConfigSection.AddKey("HTTPManager").Value = " ";
                sHTTPManager = string.Empty;
            }
            else
                sHTTPManager = mainConfigSection.GetKey("HTTPManager").Value.Trim();


            if (mainConfigSection.GetKey("PHPManager") == null
                || mainConfigSection.GetKey("PHPManager").Value == null)
            {
                mainConfigSection.AddKey("PHPManager").Value = " ";
                sPHPManager = string.Empty;
            }
            else
                sPHPManager = mainConfigSection.GetKey("PHPManager").Value.Trim();


            if (mainConfigSection.GetKey("MySQLManager") == null
                || mainConfigSection.GetKey("MySQLManager").Value == null)
            {
                mainConfigSection.AddKey("MySQLManager").Value = " ";
                sMySQLManager = string.Empty;
            }
            else
                sMySQLManager = mainConfigSection.GetKey("MySQLManager").Value.Trim();


            if (mainConfigSection.GetKey("FTPManager") == null
                || mainConfigSection.GetKey("FTPManager").Value == null)
            {
                mainConfigSection.AddKey("FTPManager").Value = " ";
                sFTPManager = string.Empty;
            }
            else
                sFTPManager = mainConfigSection.GetKey("FTPManager").Value.Trim();


            if (mainConfigSection.GetKey("SMTPManager") == null
                || mainConfigSection.GetKey("SMTPManager").Value == null)
            {
                mainConfigSection.AddKey("SMTPManager").Value = " ";
                sSMTPManager = string.Empty;
            }
            else
                sSMTPManager = mainConfigSection.GetKey("SMTPManager").Value.Trim();
            
        }
        public string getManagerDir(string name)
        {
            string installDir = string.Empty;
            if (_listManagers.ContainsKey(name))
            {
                if (_listManagers[name].GetKey("InstallDir") != null
                    || _listManagers[name].GetKey("InstallDir").Value != null)
                {
                    installDir = _listManagers[name].GetKey("InstallDir").Value;
                }
            }
            return installDir;
        }
        public void setManager(string name, string installDir)
        {
            if (!String.IsNullOrEmpty(installDir) && System.IO.Directory.Exists(installDir))
            {
                if (_listManagers.ContainsKey(name))
                {
                    if (_listManagers[name].GetKey("InstallDir") == null)
                    {
                        _listManagers[name].AddKey("InstallDir").Value = installDir;
                    }
                    else
                    {
                        _listManagers[name].GetKey("InstallDir").Value = installDir;
                    }
                }
                else
                {
                    configurationFile.AddSection(name);
                    initListManagers();
                    setManager(name, installDir);
                }
            }
        }
        private void initListManagers()
        {
            _listManagers = new Dictionary<string,IniFile.IniSection>();
            List<IniFile.IniSection> lListSections = configurationFile.Sections.Cast<IniFile.IniSection>().ToList<IniFile.IniSection>();

            foreach (IniFile.IniSection manager in lListSections)
            {
                if (manager.Name != "ServEvo")
                {
                    _listManagers.Add(manager.Name, manager);
                }
            }
        }
        public void load()
        {
            configurationFile.Load("config.ini");
        }
        public void save()
        {
            configurationFile.Save("config.ini");
        }

        ~ConfigurationData()
        {
            save();
        }
        private ConfigurationData()
        {
            if (!System.IO.File.Exists("config.ini"))
            {
                System.IO.FileStream fs = System.IO.File.Create("config.ini");
                fs.Close();
                fs.Dispose();
            }

            configurationFile = new IniFile();
            configurationFile.Load("config.ini");
            loadMainData();
            initListManagers();
        }

        public static ConfigurationData getManager()
        {
            if (ConfigurationData.manager == null)
            {
                ConfigurationData.manager = new ConfigurationData();
            }

            return ConfigurationData.manager;
        }
    }
}
